package com.gowants.pems.controller;


import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.model.SysNewLog;
import com.gowants.pems.model.SysPower;
import com.gowants.pems.model.SysUser;
import com.gowants.pems.model.SysLog;
import com.gowants.pems.service.*;
import com.gowants.pems.util.Const;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.MD5;
import com.gowants.pems.util.NetworkUtil;
import com.gowants.pems.util.RedisUtils;
import com.gowants.pems.model.LoginQrcode;
import com.gowants.pems.util.JSONMap;
import com.gowants.pems.util.QRCodeUtil;

@RestController
@RequestMapping(value = "/")
public class LoginController extends BaseController{
	@Autowired
	StringRedisTemplate stringRedisTemplate;
	@Autowired
	private SysUserService userService;
	@Autowired
	private RedisUtils redisUtils;
	
	@Autowired
	private SysPowerService sysPowerService;
	@Autowired
	private ParamsService paramsService;

	private Logger logger = LoggerFactory.getLogger(LoginController.class);
	/**
	 * key过期时间（秒）
	 */
	private final Long EXPIRATIONTIME_SECONDS = 60L;

	private final Long EXPIRATIONTIME_SECONDS_Token = 4 * 60 * 60L;

	/**
	 * key 前缀，防止和其他地方的key可以冲突
	 */
	@Value("${gowants.redisLogin}")
	private String prefix;
	
	@Value("${gowants.redisKey}")
	private String redisKey;
	
	/**
	 * session 过期时间
	 */
	@Value("${spring.redis.shiro.expire}")
	private int expire;

	/**
	 * 返回登录页面
	 */
	@RequestMapping("login.html")
	public ModelAndView loginTemplate(ModelAndView mv) {
		return mv;
	}
	/**
	 * <用户登陆接口>
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "login")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=false) //1秒内 允许请求3次
	public Map<String, Object> login(String userName, String pwd, String ip, HttpServletRequest req) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {

			String ipNew = NetworkUtil.getIpAddress(req);
			if (StringUtils.isBlank(ip)) {
				ip = ipNew;
			}

			if (StringUtils.isBlank(userName)) {
				resultMap.put("status", "400");
				resultMap.put("resultMsg", "请输入用户名");
				return resultMap;
			}
			if (StringUtils.isBlank(pwd)) {
				resultMap.put("status", "400");
				resultMap.put("resultMsg", "请输入密码");
				return resultMap;
			}

			// 前缀+ip构成key
			String key = prefix + ipNew;
			String key1 = prefix + userName;
			boolean flag = stringRedisTemplate.hasKey(key);
			boolean flag1 = stringRedisTemplate.hasKey(key1);
			// 验证是否超过3次后10次错误登录
			Map<String, Object> resultKeyMap = getKey(ip, userName, pwd, flag, flag1, key, key1, "web端", req);
			for (String keyMap : resultKeyMap.keySet()) {
				if ("success".equals(keyMap)) {
					String value = resultKeyMap.get(keyMap).toString();
					if (!Boolean.parseBoolean(value)) {
						return resultKeyMap;
					}
				}
			}
			SysUser user = new SysUser();
			user.setPwd(MD5.md5(pwd));
			user.setUserName(userName);
			List<SysUser> userList = userService.login(user);
			SysNewLog fl = new SysNewLog();
			SysLog sysLog = new SysLog();
			sysLog.setCreateTime(DateUtil.getDate());
			fl.setCreateTime(DateUtil.getDate());
			sysLog.setId(UUID.randomUUID().toString());
			fl.setId(UUID.randomUUID().toString());
			sysLog.setRemarks("ip:" + ip);
			fl.setRemarks("ip:" + ip);
			sysLog.setType(1);
			sysLog.setImportance(2);
			if (null == userList || userList.isEmpty()) {
				resultMap.put("status", "400");
				resultMap.put("resultMsg", "用户名或密码错误！");
				fl.setName("用户名:" + userName + ",密码:" + pwd + ",登录失败");
				sysLog.setName("用户名:" + userName + ",密码:" + pwd + ",登录失败");
				if (flag1) {
					// 调用次数+1
					stringRedisTemplate.boundValueOps(key1).increment(1);
					// 设置过期时间
					stringRedisTemplate.expire(key1, EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
					String sumVal = stringRedisTemplate.opsForValue().get(key1);
					int sum = Integer.valueOf(sumVal);
					if (sum > 3) {
						Map<String, Object> resultMap1 = new HashMap<String, Object>();
						resultMap1.put("resultMsg", "您已连续三次登录失败，请稍后再试");
						return resultMap1;
					}
				} else {
					// 第一次调用，所以value（调用次数）设置为1
					stringRedisTemplate.opsForValue().set(key1, "1", EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
				}
				if (flag) {
					// 调用次数+1
					stringRedisTemplate.boundValueOps(key).increment(1);
					// 设置过期时间
					stringRedisTemplate.expire(key, EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
					String sumVal = stringRedisTemplate.opsForValue().get(key);
					int sum = Integer.valueOf(sumVal);
					if (sum > 3) {
						Map<String, Object> resultMap1 = new HashMap<String, Object>();
						resultMap1.put("resultMsg", "您已连续三次登录失败，请稍后再试");
						return resultMap1;
					}
				} else {
					// 第一次调用，所以value（调用次数）设置为1
					stringRedisTemplate.opsForValue().set(key, "1", EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
				}
			} else {
				if (1 == userList.get(0).getState()) {
					resultMap.put("status", "400");
					resultMap.put("resultMsg", "账号冻结！");
					return resultMap;
				}
				if(1==userList.get(0).getIsBlackList()) {
					resultMap.put("status", "400");
					resultMap.put("resultMsg", "您已进入黑名单,不允许进入系统！");
					return resultMap;
				}
				UsernamePasswordToken token = new UsernamePasswordToken(userName, MD5.md5(pwd));
				token.setRememberMe(false);// 是否记住密码
				Subject subject = SecurityUtils.getSubject();
				subject.getSession().setTimeout(expire*1000);
				subject.login(token);// 执行成功 则表示 登陆成功
				String tCookie = MD5.getToken();
				HttpSession session = req.getSession();
				// token保存session
				session.setAttribute(Const.TOKEN_SESSION_KEY, tCookie);
				
				//将权限放入redis中
				SysPower record=new SysPower();
	    		record.setRoleId(userList.get(0).getRole());
	    		record.setIsenable(1);
	    		List<SysPower> fp=sysPowerService.select(record);
				if(fp.size()>0) {
					if(redisUtils.hasKey(redisKey+"power:"+userList.get(0).getId())) {
						redisUtils.del(redisKey+"power:"+userList.get(0).getId());
					}
					redisUtils.set(redisKey+"power:"+userList.get(0).getId(), JSON.toJSON(fp), expire);
				}
				resultMap.put("token", subject.getSession().getId().toString());
				resultMap.put("status", 200);
				resultMap.put("resultMsg", "登陆成功！");
				resultMap.put("user", userList.get(0));
				resultMap.put("resultData",  userList.get(0));
				fl.setName(
						"用户名:" + userName + ",密码:" + MD5.md5(pwd) + ",姓名:" + userList.get(0).getName() + "," + "登录成功");
				sysLog.setName("用户名:" + userName + ",密码:*******,姓名:" + userList.get(0).getName() + "," + "登录成功");
				sysLog.setCreateUid(userList.get(0).getId());
			}
			userService.saveLog(fl);
			userService.saveSysLog(sysLog);
			return resultMap;
		} catch (Exception e) {
			Map<String, Object> resultMap1 = new HashMap<String, Object>();
			resultMap1.put("status", "400");
			resultMap1.put("resultMsg", "系统错误，请联系管理员处理！！！");
			return resultMap1;
		}
	}

	/**
	 * 退出系统
	 * 
	 * @return
	 */
	@RequestMapping("logout")
	public void logout(HttpServletRequest req) {

		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		// 清空session
		req.getSession().invalidate();
	}

	/**
	 * 移动端登录
	 * 
	 * @param userName
	 * @param pwd
	 * @param ip
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "loginByMover")
	public Map<String, Object> loginByMover(String userName, String pwd, String ip, HttpServletRequest req) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String ipNew = NetworkUtil.getIpAddress(req);
			if (StringUtils.isBlank(ip)) {
				ip = ipNew;
			}
			if (StringUtils.isBlank(userName)) {
				resultMap.put("status", "400");
				resultMap.put("resultMsg", "请输入用户名");
				return resultMap;
			}
			if (StringUtils.isBlank(pwd)) {
				resultMap.put("status", "400");
				resultMap.put("resultMsg", "请输入密码");
				return resultMap;
			}
			// 前缀+ip构成key
			String key = prefix + ipNew;
			String key1 = prefix + userName;
			boolean flag = stringRedisTemplate.hasKey(key);
			boolean flag1 = stringRedisTemplate.hasKey(key1);
			// 验证是否超过3次后10次错误登录
			Map<String, Object> resultKeyMap = getKey(ip, userName, pwd, flag, flag1, key, key1, "移动端", req);
			for (String keyMap : resultKeyMap.keySet()) {
				if ("success".equals(keyMap)) {
					String value = resultKeyMap.get(keyMap).toString();
					if (!Boolean.parseBoolean(value)) {
						return resultKeyMap;
					}
				}
			}

			SysUser user = new SysUser();
			user.setPwd(MD5.md5(pwd));
			user.setUserName(userName);
			List<SysUser> userList = userService.login(user);

			SysNewLog fl = new SysNewLog();
			SysLog sysLog = new SysLog();
			sysLog.setCreateTime(DateUtil.getDate());
			fl.setCreateTime(DateUtil.getDate());
			sysLog.setId(UUID.randomUUID().toString());
			fl.setId(UUID.randomUUID().toString());
			sysLog.setRemarks("ip:" + ip);
			fl.setRemarks("ip:" + ip);
			sysLog.setType(1);
			sysLog.setImportance(2);
			if (null == userList || userList.isEmpty()) {
				resultMap.put("status", "400");
				resultMap.put("resultMsg", "用户名或密码错误！");
				fl.setName("用户名:" + userName + ",密码:" + pwd + ",移动端登录失败");
				sysLog.setName("用户名:" + userName + ",密码:" + pwd + ",移动端登录失败");
				if (flag1) {
					// 调用次数+1
					stringRedisTemplate.boundValueOps(key1).increment(1);
					// 设置过期时间
					stringRedisTemplate.expire(key1, EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
					String sumVal = stringRedisTemplate.opsForValue().get(key1);
					int sum = Integer.valueOf(sumVal);
					if (sum > 3) {
						Map<String, Object> resultMap1 = new HashMap<String, Object>();
						resultMap1.put("resultMsg", "您已连续三次登录失败，请稍后再试");
						return resultMap1;
					}
				} else {
					// 第一次调用，所以value（调用次数）设置为1
					stringRedisTemplate.opsForValue().set(key1, "1", EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
				}
				if (flag) {
					// 调用次数+1
					stringRedisTemplate.boundValueOps(key).increment(1);
					// 设置过期时间
					stringRedisTemplate.expire(key, EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
					String sumVal = stringRedisTemplate.opsForValue().get(key);
					int sum = Integer.valueOf(sumVal);
					if (sum > 3) {
						Map<String, Object> resultMap1 = new HashMap<String, Object>();
						resultMap1.put("resultMsg", "您已连续三次登录失败，请稍后再试");
						return resultMap1;
					}
				} else {
					// 第一次调用，所以value（调用次数）设置为1
					stringRedisTemplate.opsForValue().set(key, "1", EXPIRATIONTIME_SECONDS, TimeUnit.SECONDS);
				}
			} else {
				if (1 == userList.get(0).getState()) {
					resultMap.put("status", "400");
					resultMap.put("resultMsg", "账号冻结！");
					return resultMap;
				}
				UsernamePasswordToken token = new UsernamePasswordToken(userName, pwd);
				token.setRememberMe(false);// 是否记住密码
				Subject subject = SecurityUtils.getSubject();
				subject.getSession().setTimeout(expire);
				subject.login(token);// 执行成功 则表示 登陆成功
				String tCookie = MD5.getToken();
				HttpSession session = req.getSession();
				String moverToken = prefix+subject.getSession().getId().toString();
				if (redisUtils.set(moverToken, userList.get(0).getId(), EXPIRATIONTIME_SECONDS_Token)) {
//					logger.warn(redisUtils.get(moverToken).toString());
					resultMap.put("token", subject.getSession().getId().toString());
				}
				// token保存session
				session.setAttribute(Const.TOKEN_SESSION_KEY, tCookie);
				resultMap.put("status", "200");
				resultMap.put("resultMsg", "登陆成功！");
				resultMap.put("user", userList.get(0));
				fl.setName("用户名:" + userName + ",密码:" + MD5.md5(pwd) + ",姓名:" + userList.get(0).getName() + ","
						+ "Android端登录成功");
				sysLog.setName(
						"用户名:" + userName + ",密码:*******,姓名:" + userList.get(0).getName() + "," + "Android端登录成功");
				sysLog.setCreateUid(userList.get(0).getId());
			}
			userService.saveLog(fl);
			userService.saveSysLog(sysLog);
			return resultMap;
		} catch (Exception e) {
			Map<String, Object> resultMap1 = new HashMap<String, Object>();
			resultMap1.put("status", "400");
			resultMap1.put("resultMsg", "系统错误，请联系管理员处理！！！");
			return resultMap1;
		}
	}

	/***
	 * 验证是否超过三次或者十次登录失败
	 * 
	 * @param ip
	 * @param userName
	 * @param pwd
	 * @param flag
	 * @param flag1
	 * @param key
	 * @param key1
	 * @param req
	 * @return
	 */
	private Map<String, Object> getKey(String ip, String userName, String pwd, boolean flag, boolean flag1, String key,
			String key1, String sources, HttpServletRequest req) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			if (flag) {
				stringRedisTemplate.boundValueOps(key).increment(1);
				String sumVal = stringRedisTemplate.opsForValue().get(key);
				SysNewLog fl = new SysNewLog();
				fl.setId(UUID.randomUUID().toString());
				fl.setRemarks("ip:" + ip);
				fl.setCreateTime(DateUtil.getDate());
				int sum = Integer.valueOf(sumVal);
				if (sum >= 3 && sum < 10) {
					fl.setName("用户名:" + userName + ",密码:" + pwd + "," + sources + "登录失败[连续登陆" + sum + "次]");
					userService.saveLog(fl);
					resultMap.put("success", false);
					resultMap.put("resultMsg", "您已连续" + sum + "次登录失败，请1分钟后再试");
					return resultMap;
				} else if (sum == 10) {
					fl.setName("用户名:" + userName + ",密码:" + pwd + "," + sources + "登录失败[连续登陆" + sum + "次]");
					userService.saveLog(fl);
					stringRedisTemplate.expire(key, 43200L, TimeUnit.SECONDS);
					resultMap.put("success", false);
					resultMap.put("resultMsg", "您已连续十次登录失败，请24小时后再试");
					return resultMap;

				} else if (sum > 10) {
					fl.setName("用户名:" + userName + ",密码:" + pwd + "," + sources + "登录失败[连续登陆" + sum + "次]");
					userService.saveLog(fl);
					resultMap.put("success", false);
					resultMap.put("resultMsg", "您已连续" + sum + "次登录失败，请24小时后再试");
					return resultMap;

				}
			}
			// 检查key是否存在，返回boolean值
			if (flag1) {
				SysNewLog fl = new SysNewLog();
				fl.setId(UUID.randomUUID().toString());
				fl.setRemarks("ip:" + ip);
				fl.setCreateTime(DateUtil.getDate());

				stringRedisTemplate.boundValueOps(key1).increment(1);
				String sumVal1 = stringRedisTemplate.opsForValue().get(key1);
				int sum1 = Integer.valueOf(sumVal1);
				if (sum1 >= 3 && sum1 < 10) {
					fl.setName("用户名:" + userName + ",密码:" + pwd + "," + sources + "登录失败[连续登陆" + sum1 + "次]");
					userService.saveLog(fl);
					resultMap.put("success", false);
					resultMap.put("resultMsg", "您已连续" + sum1 + "次登录失败，请1分钟后再试");
					return resultMap;
				} else if (sum1 == 10) {
					stringRedisTemplate.expire(key1, 43200L, TimeUnit.SECONDS);
					fl.setName("用户名:" + userName + ",密码:" + pwd + "," + sources + "登录失败[连续登陆" + sum1 + "次]");
					userService.saveLog(fl);
					resultMap.put("success", false);
					resultMap.put("resultMsg", "您已连续十次登录失败，请24小时后再试");
					return resultMap;

				} else if (sum1 > 10) {
					fl.setName("用户名:" + userName + ",密码:" + pwd + "," + sources + "登录失败[连续登陆" + sum1 + "次]");
					userService.saveLog(fl);
					resultMap.put("success", false);
					resultMap.put("resultMsg", "您已连续" + sum1 + "次登录失败，请24小时后再试");
					return resultMap;

				}

			}
			resultMap.put("success", true);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("resultMsg", "系统错误，请联系管理员处理！！！");
			return resultMap;
		}
	}
	/**
	 * 获取登录的二维码图片
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "getLoginImage")
	public Map<String, Object>  getLoginImage(HttpServletRequest req) {
		String code="TSMS_0000432";
		try {
			String id=UUID.randomUUID().toString().replaceAll("-", "");
			String destPath = "/"+id + ".jpg";
            JSONMap js=new JSONMap();
            js.put("type", 3);
            js.put("id", id);
            String text=js.toJSONString();
            String dir = "/"+DateUtil.getDays();
    		String destPaths=paramsService.getParamValueByNumber(Const.sys_login_qrcode_path)+dir+destPath;
    		QRCodeUtil.encode(text, "", destPaths, true);
			
    		//写入redis 
    		String key=redisKey+"loginImage:"+id;
    		String url=paramsService.getParamValueByNumber(Const.sys_login_qrcode_mapping)+dir+destPath;
    		LoginQrcode lq=new LoginQrcode();
    		lq.setId(id);
    		lq.setState(0);
    		lq.setUrl(url);
    		lq.setAddress(dir+destPath);
    		boolean bo=redisUtils.setStringRedis(key, JSON.toJSON(lq).toString(),300);
			if(bo) {
				lq.setAddress("");
				code="TSMS_0000433";
				return doResponse(req, code, lq);
			}else {
				return doResponse(req, code, null);
			}
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}
	/**
	 *  通过二维码登录
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "LoginByQrcode")
	public Map<String, Object> LoginByQrcode(String request,HttpServletRequest req) {
		String code="TSMS_0000434";
		try {
			JSONMap requestBody = getRequestBody(request);
			String id = requestBody.getString("id");
			String ip="";
			if(requestBody.containsKey("ip")) {
				ip=requestBody.getString("ip");
			}
			String ipNew = NetworkUtil.getIpAddress(req);
			if (StringUtils.isBlank(ip)) {
				ip = ipNew;
			}
			Map<String, Object> resultMap = new HashMap<String, Object>();
			String key=redisKey+"loginImage:"+id;
			String spObject = redisUtils.getStringRedis(key);
			if (StringUtils.isNotBlank(spObject)) {
				LoginQrcode lq=JSONObject.parseObject(spObject, LoginQrcode.class);
				if(1==lq.getState()) {
					
					//成功
					SysUser userList = userService.selectOne(lq.getUserId());
					if (null == userList) {
						resultMap.put("status", "400");
						resultMap.put("resultMsg", "用户不存在！");
						return resultMap;
					}
					SysNewLog fl = new SysNewLog();
					SysLog sysLog = new SysLog();
					sysLog.setCreateTime(DateUtil.getDate());
					fl.setCreateTime(DateUtil.getDate());
					sysLog.setId(UUID.randomUUID().toString());
					fl.setId(UUID.randomUUID().toString());
					sysLog.setRemarks("ip:" + ip);
					fl.setRemarks("ip:" + ip);
					sysLog.setType(1);
					sysLog.setImportance(2);
					if (1 == userList.getState()) {
						resultMap.put("status", "400");
						resultMap.put("resultMsg", "账号冻结！");
						return resultMap;
					}
					if(1==userList.getIsBlackList()) {
						resultMap.put("status", "400");
						resultMap.put("resultMsg", "您已进入黑名单,不允许进入系统！");
						return resultMap;
					}
					UsernamePasswordToken token = new UsernamePasswordToken(userList.getUserName(), userList.getPwd());
					token.setRememberMe(false);// 是否记住密码
					Subject subject = SecurityUtils.getSubject();
					subject.getSession().setTimeout(expire*1000);
					subject.login(token);// 执行成功 则表示 登陆成功
					String tCookie = MD5.getToken();
					HttpSession session = req.getSession();
					// token保存session
					session.setAttribute(Const.TOKEN_SESSION_KEY, tCookie);

					//将权限放入redis中
					SysPower record=new SysPower();
					record.setRoleId(userList.getRole());
					record.setIsenable(1);
					List<SysPower> fp=sysPowerService.select(record);
					if(fp.size()>0) {
						if(redisUtils.hasKey(redisKey+"power:"+userList.getId())) {
							redisUtils.del(redisKey+"power:"+userList.getId());
						}
						redisUtils.set(redisKey+"power:"+userList.getId(), JSON.toJSON(fp), expire);
					}
					fl.setName(
							"用户名:" + userList.getUserName() + ",密码:" + userList.getPwd() + ",姓名:" + userList.getName() + "," + "扫码登录成功");
					sysLog.setName("用户名:" + userList.getUserName() + ",密码:*******,姓名:" + userList.getName() + "," + "扫描登录成功");
					sysLog.setCreateUid(userList.getId());
					userService.saveLog(fl);
					userService.saveSysLog(sysLog);
					lq.setUser(userList);
					//删除key
//					redisUtils.delStringRedis(key);
					//删除图片
					String destPaths=paramsService.getParamValueByNumber(Const.sys_login_qrcode_path)+lq.getAddress();
					File file3 = new File(destPaths);
		    		if(file3.exists())
		    		{
		    			file3.delete();
		    		}
					code="TSMS_0000438";
					return doResponse(req, code, lq);
				}else if(0==lq.getState()) {
					code="";
					lq.setAddress("");
					return doResponse(req, code, lq);
				}else if(2==lq.getState()) {
					//拒绝登录
					//删除图片
					String destPaths=paramsService.getParamValueByNumber(Const.sys_login_qrcode_path)+lq.getAddress();
					File file3 = new File(destPaths);
		    		if(file3.exists())
		    		{
		    			file3.delete();
		    		}
					code="TSMS_0000435";
					return doResponse(req, code, null);
				}
				
			}else {
				code="TSMS_0000434";
				return doResponse(req, code, null);
			}
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
		return doResponse(req, code, null);
	}
}
